﻿using System;
using System.Reflection;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using KorbeRTR.MES.Requests;

namespace KorbeRTR
{
    public partial class FormTestMES : Form
    {
        private delegate void SetTextCallback(string msg);

        private string[] RequestList = new string[]
        {
            "SendEquimentBase",
            "SendAlarmData",
            "SendAlarmTypeBase",
            
        };

        public FormTestMES()
        {
            InitializeComponent();
        }

        private void FormTestMES_Load(object sender, EventArgs e)
        {
            var type = this.GetType();

            int y = 0;
            foreach(var name in RequestList)
            {
                var btn = new Button();
                btn.Name = name;
                btn.Text = name;
                btn.Size = new Size(panel.Width - 10, 40);
                btn.Location = new Point(5, y);
                btn.Click += (EventHandler)type.GetMethod("Btn_" + name + "_Click", BindingFlags.Instance | BindingFlags.NonPublic).CreateDelegate(typeof(EventHandler), this);
                panel.Controls.Add(btn);

                y += 45;
            }
        }

        public void SetRequest(string msg)
        {
            if (TB_Request.InvokeRequired)
            {
                var callback = new SetTextCallback(SetRequest);
                Invoke(callback, new object[] { msg });
            }
            else
            {
                TB_Request.Text = msg;
            }
        }

        public void SetResponse(string msg)
        {
            if (TB_Reponse.InvokeRequired)
            {
                var callback = new SetTextCallback(SetResponse);
                Invoke(callback, new object[] { msg });
            }
            else
            {
                TB_Reponse.Text = msg;
            }
        }

        private async void Btn_SendEquimentBase_Click(object sender, EventArgs e)
        {
            try
            {
                await SendEquimentBase.Get(this);
            }
            catch(Exception err)
            {
                MessageBox.Show(err.Message + Environment.NewLine + Environment.StackTrace);
            }
        }

        private async void Btn_SendAlarmData_Click(object sender, EventArgs e)
        {
            try
            {
                await SendAlarmData.Post(this);
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message + Environment.NewLine + Environment.StackTrace);
            }
        }

        private async void Btn_SendAlarmTypeBase_Click(object sender, EventArgs e)
        {
            try
            {
                await SendAlarmTypeBase.Post(this);
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message + Environment.NewLine + Environment.StackTrace);
            }
        }
    }
}
